﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enyim.Caching;
using Enyim.Caching.Memcached;

namespace Backend.JYD.Cache
{
    internal class MemcachedCache : ICache
    {
        private static MemcachedClient m_cache = null;
        private static object _lockPad = new object();

        public MemcachedCache()
        {
            if (m_cache == null)
            {
                lock (_lockPad)
                {
                    if (m_cache == null)
                    {
                        m_cache = new MemcachedClient("memcached");
                    }
                }
            }
        }

        public void DeleteCacheItem(string cacheKey)
        {
            m_cache.Remove(cacheKey);
        }

        public T GetCachedItem<T>(string cacheKey)
        {
            try
            {
                return m_cache.Get<T>(cacheKey);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void SetCachedItem<T>(string cacheKey, T cacheObject)
        {
            m_cache.Store(StoreMode.Set, cacheKey, cacheObject, new TimeSpan(0, 0, CacheMgr.Instance.Config.CacheTime, 0));
        }

        public void ClearCache()
        {
            m_cache.FlushAll();
        }
    }
}
